package models

import (
	"fmt"
	"gitchina/kly_service/data"
	"gitchina/kly_service/errors"
	"gitchina/kly_service/util"
	"io/ioutil"
	"os"
	"path/filepath"
)

var transport_contract string
var offline_transport_contract string
var power_of_attorney string
var offline_power_of_attorney string
var letter_of_authorization string
var push_format = "推送通知：您的运单号:%s,状态发生改变，请查看！"

type ScoreAndOrderCount struct {
	ScoreCount string `json:"score_count"`
	OrderCount string `json:"order_count"`
}

func InitContract(templateDir string) {
	var err error
	if templateDir == "" {
		if templateDir, err = filepath.Abs(filepath.Dir(os.Args[0])); err != nil {
			util.GetLogger().Error("[Order-init] - error:%s", err.Error())
			return
		}
	}

	if templateDir != "" {
		if file, err := os.Open(fmt.Sprintf("%s/template/transport_contract.html", templateDir)); err == nil {
			if body, err := ioutil.ReadAll(file); err == nil {
				transport_contract = string(body)
			} else {
				util.GetLogger().Error("[Order-init] - error:%s", err.Error())
			}
			file.Close()
		} else {
			util.GetLogger().Error("[Order-init] - error:%s", err.Error())
		}

		if file, err := os.Open(fmt.Sprintf("%s/template/offline_transport_contract.html", templateDir)); err == nil {
			if body, err := ioutil.ReadAll(file); err == nil {
				offline_transport_contract = string(body)
			} else {
				util.GetLogger().Error("[Order-init] - error:%s", err.Error())
			}
			file.Close()
		} else {
			util.GetLogger().Error("[Order-init] - error:%s", err.Error())
		}

		if file, err := os.Open(fmt.Sprintf("%s/template/power_of_attorney.html", templateDir)); err == nil {
			if body, err := ioutil.ReadAll(file); err == nil {
				power_of_attorney = string(body)
			} else {
				util.GetLogger().Error("[Order-init] - error:%s", err.Error())
			}
			file.Close()
		} else {
			util.GetLogger().Error("[Order-init] - error:%s", err.Error())
		}

		if file, err := os.Open(fmt.Sprintf("%s/template/offline_power_of_attorney.html", templateDir)); err == nil {
			if body, err := ioutil.ReadAll(file); err == nil {
				offline_power_of_attorney = string(body)
			} else {
				util.GetLogger().Error("[Order-init] - error:%s", err.Error())
			}
			file.Close()
		} else {
			util.GetLogger().Error("[Order-init] - error:%s", err.Error())
		}

		if file, err := os.Open(fmt.Sprintf("%s/template/letter_of_authorization.html", templateDir)); err == nil {
			if body, err := ioutil.ReadAll(file); err == nil {
				letter_of_authorization = string(body)
			} else {
				util.GetLogger().Error("[Order-init] - error:%s", err.Error())
			}
			file.Close()
		} else {
			util.GetLogger().Error("[Order-init] - error:%s", err.Error())
		}
	}
}

type Order struct{}

func (o Order) GetInvoice(memberId, waybillId ID) (invoice InvoiceInfo, err error) {
	var mid, wid int64
	var repo data.OrderRepo
	var invoiceDto data.InvoiceDto
	if _, _, _, mid, err = memberId.Decode(); err != nil {
		err = errors.New(errors.MSG_Mbr_Invalid)
		return
	}

	if _, _, _, wid, err = waybillId.Decode(); err != nil {
		err = errors.New(errors.MSG_Order_Invalid_Waybill_ID)
		return
	}
	if invoiceDto, err = repo.GetInvoice(int(wid)); int(mid) == invoiceDto.MemberId && err == nil {
		invoice = InvoiceInfo{
			Title:         invoiceDto.Title,
			Addr:          invoiceDto.Addr,
			RecvName:      invoiceDto.RecvName,
			ZipCode:       invoiceDto.ZipCode,
			RecvPhone:     invoiceDto.RecvPhone,
			TaxNo:         invoiceDto.TaxNo,
			Amount:        invoiceDto.Amount,
			BankOfDeposit: invoiceDto.BankOfDeposit,
			BankCardNo:    invoiceDto.BankCardNo,
		}
	} else {
		if err != nil {
			util.GetLogger().Error("[model-GetInvoice] - error:%s", err.Error())
		}
	}
	return
}

func (o Order) GetScoreAndOrderCount(memberID ID) (obj ScoreAndOrderCount) {
	var err error
	var mid int64
	var repo data.OrderRepo

	if _, _, _, mid, err = memberID.Decode(); err != nil {
		util.GetLogger().Error("[model-GetScoreAndOrderCount] - error:%s", err.Error())
		return
	} else {
		if obj.ScoreCount, obj.OrderCount, err = repo.GetScoreAndOrderCount(int(mid)); err != nil {
			util.GetLogger().Error("[model-GetScoreAndOrderCount] - error:%s", err.Error())
		}
	}
	return
}
